package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/community")
public class CommunityController extends BaseController {
    @Reference
    private CommunityService communityService;
    @Reference
    private DictService dictService;
    @RequestMapping
     public String index(HttpServletRequest request, Model model){
         //对查询条件和分页参数进行封装
        Map<String, Object> filters = super.getFilters(request);
        model.addAttribute("filters",filters);
        //访问业务层获取符合条件的分页数据
        PageInfo<Community> page = this.communityService.findPage(filters);
        model.addAttribute("page",page);
        //获取区域列表areaList
        List<Dict> areaList = this.dictService.findListByDictCode("beijing");
        model.addAttribute("areaList",areaList);
        return "community/index";
     }
     @RequestMapping("/create")
     public String create( Model model){
         //获取区域列表areaList
         List<Dict> areaList = this.dictService.findListByDictCode("beijing");
         model.addAttribute("areaList",areaList);
        return "community/create";
     }
     @RequestMapping("/save")
     public String save(Community community){
        this.communityService.insert(community);
        return "common/successPage";
     }
     @RequestMapping("/edit/{id}")
     public String edit(@PathVariable Long id,Model model){
        //带着指定id小区的信息过去
         Community community = this.communityService.getById(id);
         model.addAttribute("community",community);
         //带着所有区域列表过去
         List<Dict> areaList = this.dictService.findListByDictCode("beijing");
         model.addAttribute("areaList",areaList);
        return "community/edit";
     }

    @RequestMapping("/update")
    public String update(Community community){
        this.communityService.update(community);
        return "common/successPage";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        this.communityService.delete(id);
        return "redirect:/community";
    }
}
